import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {getInfo, login, logout} from '@/api/login'
import {getToken, removeToken, setToken} from '@/utils/auth'
import {smsLogin} from "@/api/login";
import {userTabbar} from '@/utils/tabbar.js'

const baseUrl = config.baseUrl

const user = {
    state: {
        token: getToken(),
        name: storage.get(constant.name),
        avatar: storage.get(constant.avatar),
        userId: storage.get(constant.userId),
				currId: storage.get(constant.userId)
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
            storage.set(constant.name, name)
        },
        SET_USERID: (state, userId) => {
            state.userId = userId
            storage.set(constant.userId, userId)
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
            storage.set(constant.avatar, avatar)
        },
				SET_CURR_ID :(state,currId) => {
					state.currId = currId
					storage.set(constant.currId,currId)
				}
    },

    actions: {
        // 登录
        Login({commit}, userInfo) {
            let phone = userInfo.phone
            const code = userInfo.code
            return new Promise((resolve, reject) => {
                login(phone, code).then(res => {
                    setToken(res.token)
                    commit('SET_TOKEN', res.token)
										commit('SET_USERID', res.userId)
										commit('SET_NAME', res.name)
										commit('SET_AVATAR', res.avator)
										commit('SET_CURR_ID', res.currFamliy)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 获取用户信息
        GetInfo({commit, state}) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    const user = res.user
                    let nickName = user.nickName
                    let userId = user.userId
                    const avatar = (user.avatar === "" || user.avatar == null) ? require("@/static/images/user.jpg") : user.avatar
                    const username = (user.userName === "" || user.userName == null) ? "" : user.userName
                    if (res.roles && res.roles.length > 0) {
                        commit('SET_ROLES', res.roles)
                        commit('SET_PERMISSIONS', res.permissions)
                    } else {
                        commit('SET_ROLES', ['ROLE_DEFAULT'])
                    }
                    commit('SET_NAME', username)
                    commit('SET_USERID', userId)
                    commit('SET_NICKNAME', nickName)
                    commit('SET_AVATAR', avatar)
                    commit('SET_TABBAR', userTabbar)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 退出系统
        LogOut({commit, state}) {
            return new Promise((resolve, reject) => {
							commit('SET_TOKEN','')
							commit('SET_USERID', '')
							commit('SET_NAME', '')
							commit('SET_AVATAR', '')
							commit('SET_CURR_ID', '')
							removeToken()
							storage.clean()
							resolve()
          //       logout(state.token).then(() => {
          //           commit('SET_TOKEN','')
										// commit('SET_USERID', '')
										// commit('SET_NAME', '')
										// commit('SET_AVATAR', '')
										// commit('SET_CURR_FAMLIY', '')
          //           removeToken()
          //           storage.clean()
          //           resolve()
          //       }).catch(error => {
          //           reject(error)
          //       })
            })
        }
    }
}

export default user
